package cn.yunyichina.provider.pay.icbc.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.yunyichina.provider.pay.icbc.service.impl.IcbcWapServiceImpl;

public class StringFileParse {

	public StringFileParse(String strFile) {
		strFileName = strFile;

	}

	public String getAttr(String strDest) {
		System.out.println("读取参数文件中的[" + strDest + "]参数！！！");
		String strRtn = null;
		try {
			fis = StringFileParse.class.getResourceAsStream(strFileName);
			file = new BufferedReader(new InputStreamReader(fis));

			while (true) {
				String strLine = file.readLine();

				if (strLine == null)
					break;

				if (strLine.startsWith(strDest + "==")) {
					//	mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"strDest:"+strDest);
					int beginIndex = strDest.length() + 2;

					int endIndex = strLine.indexOf("/*");
					if (endIndex == -1)
						endIndex = strLine.length();
					//mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"end with"+endIndex);

					strRtn = strLine.substring(beginIndex, endIndex);
					strRtn = strRtn.trim();
					
					//	mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"strRtn:"+strRtn);
					//return strRtn;
					break;
				}

				//mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,strLine);
			}
			file.close();

		} catch (Exception e) {
			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "err read file");
		}

		return strRtn.trim();

	}
	
	public ArrayList getAttrList() {

		String sMsg ="";
		String sItem="";
		String sValue="";
		int beginIndex=0;
		int endIndex=0;
		boolean bStart=false;
		HashMap map=null;
		ArrayList list =new ArrayList();
		try {
			file = new BufferedReader(new FileReader(strFileName));
			
			while (true) {
				String strLine = file.readLine();
					
				if (strLine == null)
					break;
				strLine=strLine.trim();
				if (strLine.startsWith("[rd]")) 	//[rd]参数开始标志
				{
					list.add(map);
					map=new HashMap();
					//bStart=true;
					continue;
				}
				else if (strLine.startsWith("[in]") ) 	//包的输入部分
				{
					continue;
				}
				else if (strLine.startsWith("[Case]") ) 	//参数文件的公共部分
				{
					map=new HashMap();
					bStart=true;
					continue;
				}
				else if (strLine.startsWith("[public]") ) 	//包的公共部分
				{
					list.add(map);
					map=new HashMap();
					continue;
				}
				if( bStart )
				{
					
					beginIndex=strLine.indexOf("==");
					if(beginIndex==-1)
					{
						//System.err.println("error:"+strLine);
						continue;
					}
					sItem=strLine.substring(0,beginIndex);
					
					beginIndex = sItem.length() + 2;
					endIndex = strLine.indexOf("/*");
					if (endIndex == -1)
						endIndex = strLine.length();
					sValue = strLine.substring(beginIndex, endIndex);
					sValue = sValue.trim();
					
					map.put(sItem,sValue);	//
					sMsg=sMsg+"[ < "+mypage.FcfeMain.SIM_APP_ERP+"> ] "+sItem+"="+sValue+"\n";
					//System.err.println("item:"+sItem+"-value:"+sValue);
				}
			}
			list.add(map);	//
			mypage.FcfeMain.wR("",sMsg);
			file.close();

		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("getAttrList:"+e.getMessage());
			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "解析参数文件发生错误，请检查文件格式是否正确。");
		}

		return list;

	}
	
	public ArrayList getptAttrList() {

		String sMsg ="";
		String sItem="";
		String sValue="";
		int beginIndex=0;
		int endIndex=0;
		boolean bStart=false;
		HashMap map=null;
		ArrayList list =new ArrayList();
		try {
			file = new BufferedReader(new FileReader(strFileName));
			
			while (true) {
				String strLine = file.readLine();
					
				if (strLine == null)
					break;
				strLine=strLine.trim();
				if (strLine.startsWith("[ReqParam]")) 	//包的输入部分
				{
					//bStart=true;
					continue;
				}
				else if (strLine.startsWith("[Case]") ) 	//参数文件的公共部分
				{
					map=new HashMap();
					bStart=true;
					continue;
				}
				else if (strLine.startsWith("[ReqParamSet]") ) 	//循环参数开始标志
				{
					list.add(map);
					map=new HashMap();
					continue;
				}
				else if (strLine.startsWith("[public]") ) 	//包的公共部分
				{
					list.add(map);
					map=new HashMap();
					continue;
				}
				if( bStart )
				{
					
					beginIndex=strLine.indexOf("==");
					if(beginIndex==-1)
					{
						//System.err.println("error:"+strLine);
						continue;
					}
					sItem=strLine.substring(0,beginIndex);
					
					beginIndex = sItem.length() + 2;
					endIndex = strLine.indexOf("/*");
					if (endIndex == -1)
						endIndex = strLine.length();
					sValue = strLine.substring(beginIndex, endIndex);
					sValue = sValue.trim();

					map.put(sItem,sValue);	//
					sMsg=sMsg+"[ < "+mypage.FcfeMain.SIM_APP_ERP+"> ] "+sItem+"="+sValue+"\n";
					//System.err.println("item:"+sItem+"-value:"+sValue);
				}
			}
			list.add(map);	//
			mypage.FcfeMain.wR("",sMsg);
			file.close();

		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("getAttrList:"+e.getMessage());
			mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "解析参数文件发生错误，请检查文件格式是否正确。");
		}

		return list;

	}

	private String strFileName;
	private BufferedReader file;
	private InputStream fis;
	
}
